package GUILayer;

/** @author Gruppe 4 - Dennis Biil, Brian Smed, Kristian Henriksen og Anders Engbo
 * @version 1.0
 */
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;

import javax.swing.JFrame;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import CtrLayer.ButikCtr;
import CtrLayer.PaintingCtr;
import java.awt.Toolkit;

@SuppressWarnings("serial")
public class TabbedUI extends JFrame
{
	private JTabbedPane tabbedPane;
	private PaintingUI paintingUI;
	private ButikUI butikUI;
	private GamingUI gamingUI;
	private KundeUI kundeUI;
	private OrdreUI ordreUI;
	private ProduktServiceUI produktServiceUI;
	private static TabbedUI tabUI;

	private TabbedUI()
	{
		super();
		setIconImage(Toolkit.getDefaultToolkit().getImage(TabbedUI.class.getResource("/GUILayer/9K Logo.png")));
		
		getContentPane().setLayout(null);
		initializeTabs();
		initializeTabContainer();
		tabActionListener();
		pack();
	}

	/**
	 * Launch the application.
	 */
	public static void main(String[] args)
	{
		EventQueue.invokeLater(new Runnable()
		{
			private TabbedUI mainWindow;

			public void run()
			{
				try
				{
					mainWindow = TabbedUI.getInstance();
					mainWindow.setTitle("9K Painting");
					mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
					mainWindow.setMinimumSize(new Dimension(1024, 768));
					mainWindow.setResizable(false);
					mainWindow.setVisible(true);
				}
				catch (Exception e)
				{
					e.printStackTrace();
				}
			}
		});
	}

	public static TabbedUI getInstance()
	{
		if (tabUI == null)
		{
			tabUI = new TabbedUI();
		}
		return tabUI;
	}

	public void showTab(int tabIndex)
	{
		if (tabIndex < 5)
		{
			tabbedPane.setSelectedIndex(tabIndex);
		}
	}

	public void showButik(int ordreId)
	{
		showTab(1);
		ButikCtr.getInstance().setButikOrdre(ordreId);
		butikUI.putValueOnProduktListe();
	}

	public void showPainting(int ordreId)
	{
		showTab(0);
		PaintingCtr.getInstance().setPaintingOrdre(ordreId);
		paintingUI.putValueOnServiceListe();
	}

	public void showButikSetK(int kundeId)
	{
		showTab(1);
		ButikCtr.getInstance().setButikOrdreKunde(kundeId);
		butikUI.putValueOnProduktListe();
	}

	public void showPaintingSetK(int kundeId)
	{
		showTab(0);
		PaintingCtr.getInstance().setPaintingOrdreKunde(kundeId);
		paintingUI.putValueOnServiceListe();
	}

	private void initializeTabs()
	{
		paintingUI = new PaintingUI();
		paintingUI.setLayout(null);
		butikUI = new ButikUI();
		butikUI.setLayout(null);
		gamingUI = new GamingUI();
		gamingUI.setLayout(null);
		ordreUI = new OrdreUI();
		ordreUI.setLayout(null);
		kundeUI = new KundeUI();
		kundeUI.setLayout(null);
		produktServiceUI = new ProduktServiceUI();
		produktServiceUI.setLayout(null);
	}

	private void tabActionListener()
	{
		ChangeListener changeListener = new ChangeListener()
		{
			public void stateChanged(ChangeEvent changeEvent)
			{
				JTabbedPane sourceTabbedPane = (JTabbedPane) changeEvent.getSource();
				int index = sourceTabbedPane.getSelectedIndex();
				switch (index)
				{
				case 0:
					paintingUI.putValueOnServiceListe();
					break;
				case 1:
					butikUI.putValueOnProduktListe();
					break;
				case 2:
					gamingUI.putValueOnScreen();
					break;
				case 3:
					ordreUI.putValueOnOrdreListe();
					break;
				case 4:
					kundeUI.putValueOnScreen();
				case 5:
					produktServiceUI.putProduktValueOnScreen();
					produktServiceUI.putServiceValueOnScreen();
				default:
					break;
				}
			}
		};
		tabbedPane.addChangeListener(changeListener);
	}

	private void initializeTabContainer()
	{
		tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		tabbedPane.setBounds(10, 10, 1000, 720);
		// index 0
		tabbedPane.addTab("9k Painting", paintingUI);
		paintingUI.putValueOnServiceListe();
		// index 1
		tabbedPane.addTab("9K Butik", butikUI);
		// index 2
		tabbedPane.addTab("9K Gaming", gamingUI);
		// index 3
		tabbedPane.addTab("Ordrer", ordreUI);
		// index 4
		tabbedPane.addTab("Kunder", kundeUI);
		// index 5
		tabbedPane.addTab("Produkter/Servicer", produktServiceUI);
		tabbedPane.setVisible(true);
		getContentPane().add(tabbedPane);
	}

	// Used over all UI Elements
	static class FocusTextField extends JTextField
	{
		{
			addFocusListener(new FocusAdapter()
			{
				@Override
				public void focusGained(FocusEvent arg0)
				{
					FocusTextField.this.select(0, getText().length());
				}

				@Override
				public void focusLost(FocusEvent arg0)
				{
					FocusTextField.this.select(0, 0);
				}
			});
		}
	}
}
